package cn.edu.nbpt.campus_api.controller.blog;

import cn.edu.nbpt.campus_api.model.Page;
import cn.edu.nbpt.campus_api.model.Result;
import cn.edu.nbpt.campus_api.model.blog.entity.Article;
import cn.edu.nbpt.campus_api.service.blog.ArticleService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("blog/article")
public class ArticleController {

    @Resource
    private ArticleService articleService;

    @GetMapping("list")
    public Result<Page<Article>> selectArticleList(
            @RequestParam(required = false) String title,
            @RequestParam(required = false) Integer state,
            @RequestParam(required = false) String author,
            @RequestParam(required = false) Integer categoryId,
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            @RequestParam(required = false) LocalDate publishStartTime,
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            @RequestParam(required = false) LocalDate publishEndTime,
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize
    ) {
        return Result.success(articleService.selectArticleList(title, state, author, categoryId, publishStartTime, publishEndTime, pageNum, pageSize));
    }

    @GetMapping("{id}")
    public Result<Article> selectArticle(
            @PathVariable Integer id
    ) {
        return Result.success(articleService.selectArticle(id));
    }

    @PostMapping
    public Result<Integer> insertArticle(
            @RequestBody Article article
    ) {
        return Result.success(articleService.insertArticle(article));
    }

    @PutMapping
    public Result<Integer> updateArticle(
            @RequestBody Article article
    ) {
        return Result.success(articleService.updateArticle(article));
    }

    @DeleteMapping("{ids}")
    public Result<Integer> deleteArticle(
            @PathVariable List<Integer> ids
    ) {
        return Result.success(articleService.deleteArticle(ids));
    }
}
